<?php

/**
 * main actions.
 *
 * @package    Redsea20
 * @subpackage main
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class mainActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->setLayout('layout_main');
    //sView::SUCCESS;
    //$this->forward('default', 'module');
      
  }

  /**
   * Loads Species & Research Title
   * @param sfWebRequest $request 
   */
  public function executeMap(sfWebRequest $request)
  {             
  }

  /**
   * Loads data for Chart
   * @param sfWebRequest $request
   */
  public function executeChart_Data(sfWebRequest $request)
  {
    $params = "";// "375,374";
    if ($this->getRequestParameter('search') !=null && $this->getRequestParameter('search') != "")
    {

        $t=explode("DATE_FLAG", $this->getRequestParameter('search'));

				if (count($t)>1)
				{
					list($params, $para_year) = $t;
                 $this->chartDetail=Doctrine_Query::create()
                  //->select('odd.id, od.location')
                  ->from('observationdepthdetail odd')
		  ->leftJoin('odd.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('odd.Species s')
                  ->leftJoin('od.Unit u')
                  ->where('substr((cast(o.observationdate as char(800))),1,10) LIKE ?', '%'.$para_year.'%')
                  ->andwhere('odd.id In ('.$params.')')
                  ->orderby('o.id')
                  ->execute();
				}
				else
				{
					list($params) = $t;
                  $this->chartDetail=Doctrine_Query::create()
                  //->select('odd.id, od.location')
                  ->from('observationdepthdetail odd')
		  ->leftJoin('odd.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('odd.Species s')
                  ->leftJoin('od.Unit u')
                  ->where('odd.id In ('.$params.')')
                  ->orderby('o.id')
                  ->execute();
				}
        

    }

    $dataHolder = array();
    $uniqueUnits = array();
    $uniqueYears = array();
    $uniqueDepths = array();
    $output = "";
    $speciesList = "";
    $unitList = "";
    $yearList = "";
    $depthList = "";
    $counterArray=0;
    foreach($this->chartDetail as $item){
        
        // Filtering out unique Observation Location from returned result using data holder
        //if (!array_key_exists($item->Observationdepth->Observation["id"], $dataHolder)){
        //    $dataHolder[$item->Observationdepth->Observation["id"]] = $item->Observationdepth->Observation["location"];
        //    $output = $output.$item->Observationdepth->Observation["id"].",".$item->Observationdepth->Observation["location"].":";
        //}
        if (!in_array($item->Observationdepth->Observation["id"], $dataHolder)){
            array_push($dataHolder, $item->Observationdepth->Observation["id"]);
            $counterArray++;
            
            $output =$output.$counterArray.",".$item->Observationdepth->Observation["location"].":";
            
            
        }


        $speciesList = $speciesList.$counterArray.",".$item->Species["species"].",".((int)$item->getQuantity()).",".$item->Species["id"].":";

        // Filtering out unique Unit(s) from returned result
        if (!in_array($item->Observationdepth["unitid"], $uniqueUnits)){
            array_push( $uniqueUnits, $item->Observationdepth["unitid"]);
            $unitList = $unitList.$item->Unit["unit"].':';
        }

        // Filtering out unique Year(s) from returned result
        if (!in_array($item->Observationdepth->Observation["observationdate"], $uniqueYears)){
            array_push( $uniqueYears, $item->Observationdepth->Observation["observationdate"]);
            $yearList = $yearList.substr($item->Observationdepth->Observation["observationdate"],0,4).', ';
        }

        // Filtering out unique Depth(s) from returned result
        if (!in_array($item->Observationdepth["id"], $uniqueDepths)){
            array_push( $uniqueDepths, $item->Observationdepth["id"]);
            $depthList = $depthList.$item->Observationdepth["observationdepth"].$item->Observationdepth->Unit["unit"].', ';
        }
    }
    echo $output == "" ? $output : rtrim($output, ":").'|'.rtrim($speciesList, ":").'|'.rtrim($unitList, ":").'|'.rtrim($yearList, ", ").'|'.rtrim($depthList, ", ");
    
    return sfView::NONE;
  }
  /**
   * Draws the XML file for MARKERS
   * @param sfWebRequest $request
   * @return <type>
   */
  public function executeMap_Data(sfWebRequest $request)
  {
      $str="";
      if ($this->getRequestParameter('search')!=null && $this->getRequestParameter('search')!="")
      {
         $str=explode("OLY", $this->getRequestParameter('search'));
          //$str=$this->getRequestParameter('search');
          if ($str[0]!=null && $str[0]!="")
          {
            $this->observationList = Doctrine_Query::create()
                                ->select('o.lat,o.lng,o.location')
                                ->from('observation o')
                                ->leftJoin('o.Observationdepth od')
                                ->leftJoin('od.Observationdepthdetail odd')
                                ->where('odd.id In ('.$str[0].')')
                                ->execute();
          }
          if ($str[1]!=null && $str[1]!="")
          {
            $this->observationList_non = Doctrine_Query::create()
                                ->select('o.lat,o.lng,o.location')
                                ->from('observation o')
                                ->leftJoin('o.Observationdepth od')
                                ->where('od.id In ('.$str[1].')')
                                ->execute();
          }
      }
 else {
          $this->observationList = Doctrine_Query::create()
                                ->select('o.lat,o.lng,o.location')
                                ->from('observation o')
                                ->execute();
      }
        echo "<markers>";

        foreach($this->observationList as $r){
            echo "<marker ";
            $tag = "lat";
            echo ($tag. '="'.$r->getLat().'" ');
            $tag = "lng";
            echo ($tag. '="'.$r->getLng().'" ');
            $tag = "Location";
            echo ($tag. '="'.$r->getLocation().'" ');
            $tag = "str";
            echo ($tag. '="'.$str.'" ');
            echo (' />');
        }
        foreach($this->observationList_non as $r){
            echo "<marker ";
            $tag = "lat";
            echo ($tag. '="'.$r->getLat().'" ');
            $tag = "lng";
            echo ($tag. '="'.$r->getLng().'" ');
            $tag = "Location";
            echo ($tag. '="'.$r->getLocation().'" ');
            $tag = "str";
            echo ($tag. '="'.$str.'" ');
            echo (' />');
        }
        echo "</markers>";

        return sfView::NONE;
  }

public function executeOverlay_Data(sfWebRequest $request)
  {
      $str="";
      if ($this->getRequestParameter('search')!=null && $this->getRequestParameter('search')!="")
      {

          $str=$this->getRequestParameter('search');
            $this->overlayData = Doctrine_Query::create()
                  ->from('researchoverlay ro')
		  ->where('ro.id In ('.$str.')')
                  ->execute();
      }

        echo "<markers>";

        foreach($this->overlayData as $r){
            echo "<marker ";
            $tag = "id";
            echo ($tag. '="'.$r->getId().'" ');
            $tag = "nlat";
            echo ($tag. '="'.$r->getNlat().'" ');
            $tag = "wlng";
            echo ($tag. '="'.$r->getWlng().'" ');
            $tag = "slat";
            echo ($tag. '="'.$r->getSlat().'" ');
            $tag = "elng";
            echo ($tag. '="'.$r->getElng().'" ');
            echo (' />');
        }
        echo "</markers>";

        return sfView::NONE;
  }

  
}
